package com.lagou.controller;

import com.lagou.domain.Resource;
import com.lagou.domain.ResourceCategory;
import com.lagou.domain.ResponseResult;
import com.lagou.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ResourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 查询资源分类信息
     * @return
     */
    @RequestMapping("/findAllResourceCategory")
    public ResponseResult findAllResourceCategory() {

        List<ResourceCategory> resourceCategory = resourceCategoryService.findAllResourceCategory();

        ResponseResult result = new ResponseResult(true, 200, "获取资源类型成功", resourceCategory);

        return result;
    }

    /**
     * 添加或修改资源分类
     * @param resourceCategory
     * @return
     */
    @RequestMapping("/saveOrUpdateResourceCategory")
    public ResponseResult saveOrUpdateResourceCategory(@RequestBody ResourceCategory resourceCategory) {

        ResponseResult responseResult = null;
        if (null == resourceCategory.getId()) {
            // 表示新增
            resourceCategoryService.saveResourceCategory(resourceCategory);
            responseResult = new ResponseResult(true, 200, "添加资源分类成功", null);
        } else {
            // id不为null, 表示修改
            resourceCategoryService.updateResourceCategory(resourceCategory);
            responseResult = new ResponseResult(true, 200, "修改资源分类成功", null);
        }

        return responseResult;
    }

    /**
     * 根据id, 删除资源分类
     * @param id
     * @return
     */
    @RequestMapping("/deleteResourceCategory")
    public ResponseResult deleteResourceCategoryById(Integer id) {

        resourceCategoryService.deleteResourceCategoryById(id);

        return new ResponseResult(true, 200, "删除资源分类成功", null);
    }
}
